<template>
  <el-dialog title="分配权限" :visible="showDialogperm" @close="hClose">
    <el-tree
      ref="permList"
      node-key="id"
      :data="permList"
      default-expand-all
      show-checkbox
      check-strictly
      :props="{label:'name'}"
    />
    <el-button size="mini" type="primary" @click="btnOK">
      <!--  -->
      保存
    </el-button>
    <el-button size="mini" @click="hClose">
      取消
      <!--  -->
    </el-button>
    <!-- <p>{{ roleId }}</p> -->
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { arr2tree } from '@/utils'
import { assignPerm, getRoleDetail } from '@/api/role'
export default {
  props: {
    showDialogperm: {
      type: Boolean,
      required: true
    },
    roleId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      permList: []
    }
  },
  created() {
    this.getRolePermission()
  },
  methods: {
    async btnOK() {
      const permIds = this.$refs.permList.getCheckedKeys()
      // console.log('当前选中的节点数组是', permIds)
      const res = await assignPerm({
        id: this.roleId,
        permIds
      })
      console.log('保存角色的权限点的结果是', res)

      // 提示
      this.$message.success('保存角色的权限成功')
      // 关闭弹层
      this.hClose()
    },
    async getPermissionByid() {
      const res = await getRoleDetail(this.roleId)
      console.log(res)
      this.$refs.permList.setCheckedKeys(res.data.permIds)
    //   this.permList = res.data.permIds
    },
    hClose() {
      this.$emit('update:showDialogperm', false)
    },
    async getRolePermission() {
      // 获取权限数据
      const res = await getPermissionList()
      //   console.log(res)
      this.permList = arr2tree(res.data, 0)
    }
  }
}
</script>

<style>

</style>
